package com.jeesite.modules.dataScreen.utils;

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;

@Component
public class ExecutorUtils {
    private static ThreadPoolExecutor threadPoolExecutor;

    public ExecutorUtils(ThreadPoolExecutor threadPoolExecutor) {
        ExecutorUtils.threadPoolExecutor = threadPoolExecutor;
    }

    public static void execute(Runnable runnable, CountDownLatch countDownLatch) {
        threadPoolExecutor.execute(() -> {
            try {
                runnable.run();
            } finally {
                if (Objects.nonNull(countDownLatch)) {
                    countDownLatch.countDown();
                }
            }
        });
    }
}
